package com.ruicar.afs.cloud.afscase.job.loan;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.afscase.channel.entity.ChannelAuthorizeRegion;
import com.ruicar.afs.cloud.afscase.channel.entity.ChannelBaseInfo;
import com.ruicar.afs.cloud.afscase.channel.service.ChannelAuthorizeRegionService;
import com.ruicar.afs.cloud.afscase.channel.service.ChannelBaseInfoService;
import com.ruicar.afs.cloud.afscase.loandealerfirstmortgage.entity.LoanDealerFirstMortgage;
import com.ruicar.afs.cloud.afscase.loandealerfirstmortgage.service.LoanDealerFirstMortgageService;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.job.core.biz.model.ReturnT;
import com.ruicar.afs.cloud.common.job.core.handler.annotation.AfsJob;
import com.ruicar.afs.cloud.common.job.core.handler.annotation.AfsJobHandler;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.WhetherEnum;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @description: 合作商展业城市信息同步定时任务
 * @author: rongji.zhang
 * @date: 2020/8/24 17:53
 */
@Slf4j
@Component
@AfsJob
@RestController
@AllArgsConstructor
@ApiOperation(value = "合作商展业城市信息同步定时任务")
public class DealerFirstMortgageJob {

    private ChannelAuthorizeRegionService channelAuthorizeRegionService;

    private ChannelBaseInfoService channelBaseInfoService;

    private LoanDealerFirstMortgageService loanDealerFirstMortgageService;
    /**
     * 同步合作商展业城市数据
     *
     * @param
     * @return
     */
    @AfsJobHandler(value = "synchronizationDealer")
    public ReturnT<String> synchronizationDealer(String param) {
        try {
            log.info("************************合作商展业城市信息同步定时任务执行开始*******************************");
            List<ChannelAuthorizeRegion> channelAuthorizeRegionList = channelAuthorizeRegionService.list(Wrappers.<ChannelAuthorizeRegion>query().lambda());
            if(CollectionUtils.isNotEmpty(channelAuthorizeRegionList)){
                channelAuthorizeRegionList.forEach(channelAuthorizeRegion -> {
                    ChannelBaseInfo channelBaseInfo = channelBaseInfoService.getOne(Wrappers.<ChannelBaseInfo>query().lambda()
                            .eq(ChannelBaseInfo::getId,channelAuthorizeRegion.getChannelId().toString()),false);
                    if(ObjectUtils.isNotEmpty(channelBaseInfo)){
                        LoanDealerFirstMortgage loanDealerFirstMortgage = loanDealerFirstMortgageService.getOne(Wrappers.<LoanDealerFirstMortgage>query().lambda()
                                .eq(LoanDealerFirstMortgage::getDealerNo,channelBaseInfo.getChannelCode())
                                .eq(LoanDealerFirstMortgage::getProvinceCode,channelAuthorizeRegion.getParentId())
                                .eq(LoanDealerFirstMortgage::getCityCode,channelAuthorizeRegion.getCode()),false);
                        if(ObjectUtils.isEmpty(loanDealerFirstMortgage)){
                            LoanDealerFirstMortgage newLoanDealerFirstMortgage = new LoanDealerFirstMortgage();
                            newLoanDealerFirstMortgage.setDealerNo(channelBaseInfo.getChannelCode());
                            newLoanDealerFirstMortgage.setDealerName(channelBaseInfo.getChannelFullName());
                            newLoanDealerFirstMortgage.setChannelType(channelBaseInfo.getChannelType());
                            if(channelAuthorizeRegion.getCode().length()==6){
                                newLoanDealerFirstMortgage.setCityCode(channelAuthorizeRegion.getCode());
                                newLoanDealerFirstMortgage.setCityName(channelAuthorizeRegion.getTitle());
                                newLoanDealerFirstMortgage.setProvinceCode(channelAuthorizeRegion.getParentId());
                            } else {
                                newLoanDealerFirstMortgage.setCityName(channelAuthorizeRegion.getTitle());
                                newLoanDealerFirstMortgage.setCityCode(channelAuthorizeRegion.getCode());
                                newLoanDealerFirstMortgage.setProvinceName(channelAuthorizeRegion.getTitle());
                                newLoanDealerFirstMortgage.setProvinceCode(channelAuthorizeRegion.getCode());
                            }
                            newLoanDealerFirstMortgage.setIsFirstMortgage(WhetherEnum.NO.getCode());
                            loanDealerFirstMortgageService.save(newLoanDealerFirstMortgage);
                        }
                    }
                });
            }
            log.info("************************合作商展业城市信息同步定时任务执行结束*******************************");
            return ReturnT.SUCCESS;
        }catch (Exception e){
            log.error("*************************************************合作商展业城市信息同步定时任务执行失败*********************************************************",e);
            return ReturnT.FAIL;
        }
    }
}
